/**
 * Copyright (c) 2011 - 2012 National Library of Medicine
 *
 * Contact: tomasz.adamusiak@nih.gov;
 * 
 * This file is part of LOwlINC
 * 
 * LOwlINC is free software: you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 3 of the License, or (at your option) any
 * later version.
 * 
 * LOwlINC is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU Lesser General Public License along
 * with LOwlINC. If not, see <http://www.gnu.org/licenses/>.
 */
package gov.nih.nlm.lowlinc.csv;

import gov.nih.nlm.lowlinc.accessionwrapper.LOINCTermID;
import gov.nih.nlm.lowlinc.bean.LOINCTerm;
import gov.nih.nlm.lowlinc.core.LowlincConfig;
import gov.nih.nlm.lowlinc.owlapi.LowlincException;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;

import au.com.bytecode.opencsv.CSVReader;

/**
 * @author Tomasz Adamusiak
 * 
 */
public class LOINCTermsCSVParser {
	private static final Logger log = Logger
			.getLogger(LOINCTermsCSVParser.class);
	private String[] headerCSV;
	private final Map<LOINCTermID, LOINCTerm> terms = new HashMap<>();

	public Map<LOINCTermID, LOINCTerm> getTerms() {
		return terms;
	}

	public LOINCTermsCSVParser() throws LowlincException {
		try {
			// Need to explicitly define the delimiter as otherwise the comments
			// in the beginning throw the parser off
			CSVReader reader = new CSVReader(new FileReader(new File(
					LowlincConfig.getString("LOINCTermsCSVParser.path"))), '\t'); //$NON-NLS-1$

			// skip comments until <----Clip Here for Data----->
			String[] nextLine;
			while ((nextLine = reader.readNext()) != null) {
				if (nextLine[0].startsWith("<----Clip Here for Data----->")) {
					log.debug("Skipped comment lines. LOINC terms parsing started");
					break;
				}
			}

			headerCSV = reader.readNext();

			while ((nextLine = reader.readNext()) != null) {
				LOINCTerm term = bindStringToBean(nextLine);
				terms.put(term.getTermID(), term);
			}
			log.info(terms.size() + " LOINC Terms loaded");
		} catch (IOException e) {
			throw new LowlincException(e);
		}
	}

	// TODO: this code is essentially a copy of the one in
	// the other parser, consider generalising
	private LOINCTerm bindStringToBean(String[] line) throws LowlincException {
		try {
			LOINCTerm bean = new LOINCTerm();

			for (int i = 0; i < headerCSV.length; i++) {
				Method meth = bean.getClass().getMethod("set" + headerCSV[i],
						String.class);
				meth.invoke(bean, line[i]);
			}
			return bean;

		} catch (NoSuchMethodException | IllegalAccessException
				| IllegalArgumentException | InvocationTargetException e) {
			throw new LowlincException(e);
		}
	}
}
